/*******************************************************************************
 *
 *    Copyright (c) BaiNa Co. Ltd
 *
 *    FlipBoard
 *	  DisplayHelper.java
 *    DisplayHelper
 *
 *    @author: DanLiu
 *    @since:  2010-12-15
 *    @version: 1.0
 *
 ******************************************************************************/

package small.hero.pageview;

import java.util.Random;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Surface;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 *
 */
public class DisplayHelper {

    public final static float DENSITY = Resources.getSystem().getDisplayMetrics().density;

    public final static int DEVICE_PHONE = -1;
    public final static int DEVICE_7INCH = 0;
    public final static int DEVICE_10INCH = 1;

    /*
     * This is galaxy tab size calculated by DPI, we don't use xDPI or yDPI as some manufacture's
     * xDPI and yDPI are very strange.
     */
    private final static float GALAXY_TAB_SIZE = 5f;

    /*
     * This is a middle value for Milestone and Galaxy Tab. We consider devices smaller than this as Phone,
     *  larger than this as Pad.
     */
    private final static float PAD_PHONE_THREDHOLD = 4.5f;

    /*
     * Whether it's a 7inch device or > 7inch device.
     */
    public static int getDeviceType(Context context)
    {
        final float width = screenWidthPhysical(context);
        final float height = screenHeightPhysical(context);
        final float physicalSize = (float) Math.sqrt(width * width + height * height);
        if(physicalSize < PAD_PHONE_THREDHOLD)
        {
            return DEVICE_PHONE;
        }
        else if (physicalSize < GALAXY_TAB_SIZE)
        {
            return DEVICE_7INCH;
        }
        else
        {
            return DEVICE_10INCH;
        }
    }

    public static float screenHeightPhysical(Context context)
    {
        final Display display = getDisplay(context);
        final DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        return display.getHeight() / metrics.densityDpi;
    }

    public static float screenWidthPhysical(Context context)
    {
        final Display display = getDisplay(context);
        final DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        return display.getWidth()/ metrics.densityDpi;
    }

    public static int dipToPixel(int dip){
        return (int) (dip * DENSITY + 0.5f);
    }

    public static int dipToPixel(float dip){
        return (int) (dip * DENSITY + 0.5f);
    }

    public static int pixelToDip(int pixel)
    {
        return (int)((pixel - 0.5f)/DENSITY);
    }

    public static int screenWidthDip(Context context)
    {
        if (sScreenWidthDip == 0)
        {
            final WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
            final int widthPixels = windowManager.getDefaultDisplay().getWidth();
            sScreenWidthDip = pixelToDip(widthPixels);
        }

        return sScreenWidthDip;
    }

    public static int screenHeightDip(Context context)
    {
        if (sScreenHeightDip == 0)
        {
            final WindowManager windowManager = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
            final int heightPixels = windowManager.getDefaultDisplay().getHeight();
            sScreenHeightDip = pixelToDip(heightPixels);
        }

        return sScreenHeightDip;
    }

    /*
     * For debug purpose.
     */
    public static int getRandomColor()
    {
        final int colors[] = {Color.BLACK,Color.RED,Color.WHITE,Color.GRAY,Color.GREEN};
        final Random random = new Random();
        final int index = random.nextInt(colors.length);
        return colors[index];
    }

    public static int getRotation(Context context) {
        final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return windowManager.getDefaultDisplay().getRotation();
    }

    public static boolean isLandscape(Context context) {
        final int rotation = getRotation(context);
        return Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation;
    }

    public static Display getDisplay(Context context) {
        final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return windowManager.getDefaultDisplay();
    }

    private static int sScreenWidthDip;
    private static int sScreenHeightDip;

    public static boolean isHardwareAccelerated(Canvas canvas) {
        if(Build.VERSION.SDK_INT >= 11) {
            try {
                return (Boolean) Canvas.class.getDeclaredMethod("isHardwareAccelerated").invoke(canvas);
            } catch (final Exception e) {
            }
        }
        return false;
    }

    private static final int LAYER_TYPE_NONE = 0;
    private static final int LAYER_TYPE_SOFTWARE = 1;
    private static final int LAYER_TYPE_HARDWARE = 2;
    public static void setHardwareAccelerated(View view) {
        if(Build.VERSION.SDK_INT >= 11) {
            try {
                View.class.getDeclaredMethod("setLayerType", int.class, Paint.class).invoke(view, LAYER_TYPE_HARDWARE, null);
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void setSoftwareRendering(View view) {
        if(Build.VERSION.SDK_INT >= 11) {
            try {
                View.class.getDeclaredMethod("setLayerType", int.class, Paint.class).invoke(view, LAYER_TYPE_SOFTWARE, null);
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }

//    public static void setHardwareAccelerated(Window window) {
//        if(Build.VERSION.SDK_INT >= 11) {
//            window.addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
//        }
//    }
}
